package com.example.demo.controller;

import com.example.demo.model.Person;
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@RestController
public class TestController {

    /**
     * 属性注入*/
    //    @Autowired
    //    private TestService testService;

    /**
     * 构造方法注入*/
    //    @Autowired
    //    public TestController(TestService testService) {
    //        this.testService = testService;
    //    }
    //
    //    public TestController() {
    //
    //    }

//    private TestService testService;

    /**
     * Setter注入*/
    //    @Autowired
    //    public void setTestService(TestService testService) {
    //        this.testService = testService;
    //    }

    //    private Person studentOne;

    //    @Autowired
    //    public void setStudent(Person studentOne) {
    //        this.studentOne = studentOne;
    //    }


    /**
     * 使用@PostConstruct注解可以在程序初始化之后立马执行所修饰的方法
     * 如此一来就不需要我们再去访问地址，或是获取对象后再去调用，比较适合框架学习*/
    //    @PostConstruct
    //    public void doController() {
    //        testService.doService();
    //        System.out.println("TestController running...");
    //        // 修改对象
    //        // -- 成功调用setter进行修改
    //        Person person = new Person();
    //        Person studentTwo = person.studentTwo();
    //        setStudent(studentTwo);
    //
    //        System.out.println(studentOne.toString());
    //    }

    /**
     * 使用属性注入的方式注入被final修饰的属性
     * 观察会出现什么情况*/
    //    @Autowired
    //    private final TestService testService;

    //    public void doController() {
    //        testService.doService();
    //        System.out.println("Controller running...");
    //    }

    /**
     * @Autowired注解存在的问题以及解决方法*/

//    @Qualifier("studentOne")
//    @Autowired
//    private Person student;

    @Resource(name = "studentOne")
    private Person student;

    @PostConstruct
    public void doTestController() {
        System.out.println("Controller running...");
        System.out.println(student.toString());
    }
}
